<template>
  <div class="main">
    <!-- <el-card> -->
    <div style="margin-bottom: 50px;">
      <h4>修改密码</h4>
      <!-- <el-button style="float: right; padding: 3px 0" type="text">操作按钮</el-button> -->
    </div>
    <div>
      <!-- <div style="margin-left: 10px;margin-bottom: 30px;">基本信息</div> -->
      <el-form ref="form" :rules="resetFormRules" :model="form" label-width="120px" class="demo-form-inline">
        <el-form-item label="原密码：" required prop="old_password">
          <el-input v-model="form.old_password" show-password style="width: 400px;" placeholder="输入原登录密码" />
        </el-form-item>
        <el-form-item label="新密码：" required prop="password">
          <el-input v-model="form.password" show-password style="width: 400px;" placeholder="输入新的登录密码">手机号码</el-input>
        </el-form-item>
        <el-form-item label="新密码：" required prop="password_confirmation">
          <el-input v-model="form.password_confirmation" show-password style="width: 400px;" placeholder="再次输入登录密码">手机号码</el-input>
        </el-form-item>
        <el-form-item>
          <el-button style="width: 120px" type="primary" @click="onSubmit">保存</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- </el-card> -->
  </div>
</template>
<script>
import { updatePassword } from '@/api/authority'
export default {
  data() {
    const validatePassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入密码'))
      } else if (value.toString().length < 6 || value.toString().length > 18) {
        callback(new Error('密码长度为6-18位'))
      } else {
        callback()
      }
    }
    const validatePassword2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.form.password) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }
    return {
      form: {
        old_password: '',
        password: '',
        password_confirmation: ''
      },
      resetFormRules: {
        old_password: [{ required: true, trigger: 'blur', message: '请输入旧密码' }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        password_confirmation: [{ required: true, trigger: 'blur', validator: validatePassword2 }]
      }
    }
  },
  methods: {
    onSubmit() {
      this.$refs.form.validate(vaild => {
        if (vaild) {
          updatePassword(this.form).then(res => {
            if (res.status === 200) {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.logout() // 调用跳转到登陆页的方法
              // setTimeout(() => {
              // }, 2000)
              // this.$router.push('/userCenter')
            }
          })
        }
      })
    },
    async logout() {
      await this.$store.dispatch('user/logout')
      // this.$router.push(`/login?redirect=${this.$route.fullPath}`)
      // location.href = '/'
      this.$router.push(`/login`)
    }
  }
}
</script>
<style lang="scss" scoped>
.main {
  padding: 20px 0 0 30px;
}
</style>
